package com.cv.platform.service.comment;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.mybatis.core.query.QueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.infra.api.file.FileApi;
import com.cv.infra.api.websocket.WebSocketSenderApi;
import com.cv.platform.controller.platform.comment.vo.record.CvCommentCreateReqVO;
import com.cv.platform.controller.platform.comment.vo.record.CvCommentReqVO;
import com.cv.platform.controller.platform.comment.vo.record.CvCommentRespVO;
import com.cv.platform.controller.platform.message.vo.conversation.CvConversationSaveReqVO;
import com.cv.platform.controller.platform.message.vo.record.CvMessageSaveReqVO;
import com.cv.platform.dal.dataobject.comment.CvCommentDO;
import com.cv.platform.dal.mapper.comment.CvCommentMapper;
import com.cv.platform.service.message.CvConversationService;
import com.cv.platform.service.message.CvMessageService;
import com.cv.platform.service.report.CvReportService;
import com.cv.platform.service.video.CvVideoService;
import com.cv.system.api.user.UserApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 视频评论业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvCommentServiceImpl implements CvCommentService {

    @Resource
    private CvCommentMapper cvCommentMapper;

    @Resource
    private CvCommentLikeService cvCommentLikeService;

    @Resource
    private CvCommentDislikeService cvCommentDislikeService;

    @Resource
    private CvVideoService cvVideoService;

    @Resource
    private CvCommentReplyService cvCommentReplyService;

    @Resource
    private CvConversationService cvConversationService;

    @Resource
    private CvMessageService cvMessageService;

    @Resource
    private CvReportService cvReportService;

    @DubboReference
    private FileApi fileApi;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private WebSocketSenderApi socketSenderApi;

    @Override
    public void createComment(CvCommentCreateReqVO reqVO) {
        CvCommentDO cvCommentDO = BeanUtils.toBean(reqVO, CvCommentDO.class);
        cvCommentDO.setUserId(SecurityFrameworkUtil.getLoginUserId());
        cvCommentMapper.insert(cvCommentDO);
        cvVideoService.addVideoCommentCount(reqVO.getVideoId());
    }

    @Override
    public PageResult<CvCommentRespVO> getCommentPage(CvCommentReqVO reqVO) {
        PageResult<CvCommentRespVO> pageResult = cvCommentMapper.selectPage(reqVO);

        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty();
        }

        pageResult.getList().forEach(item -> {
            item.setAvatar(userApi.getAvatarById(item.getUserId()));
            item.setAvatarUrl(fileApi.getUrl(item.getAvatar()));
            item.setNickname(userApi.getNicknameById(item.getUserId()));
            item.setIsLike(cvCommentLikeService.getCommentLikeStatus(item.getId()));
            item.setIsDislike(cvCommentDislikeService.getCommentDislikeStatus(item.getId()));
            item.setLikeCount(cvCommentLikeService.getCommentLikeCount(item.getId()));
            item.setDislikeCount(cvCommentDislikeService.getCommentDislikeCount(item.getId()));
        });

        return pageResult;
    }

    @Override
    public Long getVideoIdByCommentId(Long commentId) {
        if (ObjUtil.isNotEmpty(commentId)) {
            QueryWrapperX<CvCommentDO> wrapperX = new QueryWrapperX<CvCommentDO>()
                    .eq("id", commentId)
                    .eq("deleted", 0);
            return cvCommentMapper.selectOne(wrapperX).getVideoId();
        }
        return null;
    }

    @Override
    public String getCommentContent(Long commentId) {
        if (ObjUtil.isNotEmpty(commentId)) {
            CvCommentDO cvCommentDO = cvCommentMapper.selectById(commentId);
            if (ObjUtil.isEmpty(cvCommentDO)) {
                return cvCommentReplyService.getCommentReplyContent(commentId);
            }
            return cvCommentDO.getContent();
        }
        return StrUtil.EMPTY;
    }

    @Override
    public void deleteAllComment(Long userId) {
        if (ObjUtil.isNotEmpty(userId)) {
            LambdaQueryWrapperX<CvCommentDO> wrapper = new LambdaQueryWrapperX<CvCommentDO>()
                    .eq(CvCommentDO::getUserId, userId);
            cvCommentMapper.delete(wrapper);
        }
    }

    @Override
    public void deleteComment(Long commentId) {
        CvCommentDO cvCommentDO = cvCommentMapper.selectById(commentId);
        Long userId = null;
        if (ObjUtil.isNotEmpty(cvCommentDO)) {
            userId = cvCommentDO.getUserId();
        } else {
            userId = cvCommentReplyService.getSenderIdByReplyId(commentId);
        }

        if (ObjUtil.isEmpty(userId)) {
            return;
        }

        Long conversationId = cvConversationService.getConversationId(SecurityFrameworkUtil.getLoginUserId(), userId);
        if (ObjUtil.isEmpty(conversationId)) {
            conversationId = cvConversationService.saveConversation(new CvConversationSaveReqVO()
                    .setTargetUserId(userId)
            );
        }

        cvMessageService.saveMessage(new CvMessageSaveReqVO()
                .setContent("您的评论：'" + getCommentContent(commentId) + "' 因内容不符合平台规范，已被删除。")
                .setConversationId(conversationId)
                .setReceiverId(userId)
                .setSenderId(SecurityFrameworkUtil.getLoginUserId()));

        // 删除相关的举报内容
        cvReportService.deleteAllReport(commentId);

        cvCommentMapper.deleteById(commentId);
        cvCommentReplyService.deleteCommentReply(commentId);
    }

}
